﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TicTacToe
{
    public enum BoardPlayer
    {
        NoPlayer = 0,
        Player = 1,
        Computer = 2
    }

    [Serializable]
    public class Board : IEnumerable<BoardPlayer>, ICloneable
    {
        private int[] board;

        public Board()
        {
            this.board = new int[] { 0, 0, 0, 
                                     0, 0, 0, 
                                     0, 0, 0 };
        }

        public BoardPlayer this[int index]
        {
            get
            {
                return (BoardPlayer)this.board[index];
            }
            set
            {
                this.board[index] = (int)value;
            }
        }

        public BoardPlayer this[int x, int y]
        {
            get
            {
                return (BoardPlayer)this.board[y * 3 + x];
            }
            set
            {
                this.board[y * 3 + x] = (int)value;
            }
        }

        public bool IsEmpty()
        {
            foreach (int i in this.board)
            {
                if (i != 0)
                {
                    return false;
                }
            }

            return true;
        }

        public bool IsFull()
        {
            foreach (int i in this.board)
            {
                if (i == 0)
                {
                    return false;
                }
            }

            return true;
        }

        public int[] GetValidMoves()
        {
            List<int> moves = new List<int>();
            for (int i = 0; i < 9; i++)
            {
                if (board[i] == 0)
                {
                    moves.Add(i);
                }
            }

            return moves.ToArray();
        }

        public bool Evaluate(BoardPlayer player)
        {
            int x = 0;

            //check columns
            for (int i = 0; i < 3; i++)
            {
                x = 0;
                for (int j = 0; j < 3; j++)
                {
                    if ((int)this[i, j] == (int)player) x++;
                }

                if (x == 3)
                {
                    return true;
                }
            }

            //check rows
            for (int i = 0; i < 3; i++)
            {
                x = 0;
                for (int j = 0; j < 3; j++)
                {
                    if ((int)this[j, i] == (int)player)
                    {
                        x++;
                    }
                }

                if (x == 3)
                {
                    return true;
                }
            }

            //check diagonal
            x = 0;
            for (int i = 0; i < 3; i++)
            {
                if ((int)this[i, i] == (int)player)
                {
                    x++;
                }

                if (x == 3)
                {
                    return true;
                }
            }

            //check other diagonal
            x = 0;
            for (int i = 2, j = 0; i >= 0; i--, j++)
            {
                if ((int)this[i, j] == (int)player)
                {
                    x++;
                }

                if (x == 3)
                {
                    return true;
                }
            }

            return false;
        }

        public object Clone()
        {
            Board newBoard = (Board)base.MemberwiseClone();
            newBoard.board = (int[])board.Clone();
            return newBoard;
        }

        public IEnumerator<BoardPlayer> GetEnumerator()
        {
            BoardPlayer[] newBoard = Array.ConvertAll<int, BoardPlayer>(board,
                new Converter<int, BoardPlayer>(delegate(int i) { return (BoardPlayer)i; }));

            return (newBoard as IEnumerable<BoardPlayer>).GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}